##########
#Countering Authoritarian Behavior in Democracies
#Sara B. Hobolt, Moritz Osnabruegge
#This script produces Figures 1, A1, and A6-A9
#########


library(foreign)
library(gdata)
library(lmtest)
library(ggplot2)
library(multiwayvcov)

data <- read.dta("data_analysis.dta")

source("functions.R")
source("themes.R")

a <- as.data.frame(matrix(c("Controversy:","   (had an extramarital affair)", 1,NA,1,NA,1,NA,1,NA), nrow=2, ncol=5))
colnames(a) <- c("var","pe","se","ul","ll")
b <- as.data.frame(matrix(c("Actor Reacting:","   (MPs from another party)", 1,NA,1,NA,1,NA,1,NA), nrow=2, ncol=5))
colnames(b) <- c("var","pe","se","ul","ll")
c <- as.data.frame(matrix(c("Reaction:","   (did not react)", 1,NA,1,NA,1,NA,1,NA), nrow=2, ncol=5))
colnames(c) <- c("var","pe","se","ul","ll")
d <- as.data.frame(matrix(c("Gender:","   (male)", 1,NA,1,NA,1,NA,1,NA), nrow=2, ncol=5))
colnames(d) <- c("var", "pe","se","ul","ll")
e <- as.data.frame(matrix(c("Party:","   (Labour)", 1,NA,1,NA,1,NA,1,NA), nrow=2, ncol=5))
colnames(e) <- c("var","pe","se","ul","ll")
f <- as.data.frame(matrix(c("Experience:","   (no minister)", 1,NA,1,NA,1,NA,1,NA), nrow=2, ncol=5))
colnames(f) <- c("var","pe","se","ul","ll")



#########
##Figure 1
#########

result1 <- lm(chosen ~ threat_britain + court_crisis + no_parliament + exclude_journalists + online_harrasment + expenses + ignored_messages + actor_same_party + expelled + criticized + defended + refused_work + female + conservative + minister, data=data)
vcov_id1 <- cluster.vcov(model=result1, data$ID)
coef1 <- as.data.frame.matrix(coeftest(result1, vcov_id1)) 

out1 <- prep_data(coef1)

out1$var <- factor(out1$var,levels=unique(out1$var)[length(out1$var):1])

p1 <- ggplot(data=out1) + coord_flip(ylim=c(-.25,.05)) +
      geom_hline(yintercept=0,linewidth=.5,colour="darkgrey",linetype="dotted") +
      geom_pointrange(mapping=aes(y=pe,x=var,ymin=ll,ymax=ul),size=.5,na.rm=T) +
      scale_y_continuous(name="Change in Probability of Selecting Candidate",breaks=round(seq(-.25,.05,.05),0.5)) +
      scale_x_discrete(name="") +
      theme_bw1()

pdf("fig_1.pdf",width=9,height=5) 
print(p1)
dev.off()



#########
##Figure A9
#########

result2 <- lm(rating2 ~ threat_britain + court_crisis + no_parliament + exclude_journalists + online_harrasment + expenses + ignored_messages + actor_same_party + expelled + criticized + defended + refused_work + female + conservative + minister, data=data)
vcov_id2 <- cluster.vcov(result2, data$ID)
coef2 <- as.data.frame.matrix(coeftest(result2, vcov_id2))

out2 <- prep_data(coef2)

out2$var <- factor(out2$var,levels=unique(out2$var)[length(out2$var):1])

p2 <- ggplot(data=out2) + coord_flip(ylim=c(-.15,.05)) +
      geom_hline(yintercept=0,linewidth=.5,colour="darkgrey",linetype="dotted") +
      geom_pointrange(mapping=aes(y=pe,x=var,ymin=ll,ymax=ul),size=.5,na.rm=T) +
      scale_y_continuous(name="Change: Candidate Rating",breaks=round(seq(-.15,.05,.05),0.5)) +
      scale_x_discrete(name="") +
      theme_bw1()

pdf("fig_A9.pdf",width=9,height=5) 
print(p2)
dev.off()



#########
##Figure A1
#########

result3 <- lm(chosen ~ threat_britain*expelled + threat_britain*criticized + threat_britain*defended + threat_britain*refused_work + court_crisis*expelled + court_crisis*criticized + court_crisis*defended + court_crisis*refused_work + no_parliament*expelled + no_parliament*criticized + no_parliament*defended + no_parliament*refused_work + exclude_journalists*expelled + exclude_journalists*criticized + exclude_journalists*defended + exclude_journalists*refused_work + online_harrasment*expelled + online_harrasment*criticized + online_harrasment*defended + online_harrasment*refused_work +  expenses*expelled + expenses*criticized + expenses*defended + expenses*refused_work + ignored_messages*expelled + ignored_messages*criticized + ignored_messages*defended + ignored_messages*refused_work  + actor_same_party + female + conservative + minister, data=data)
vcov_id3 <- cluster.vcov(result3, data$ID)
coef3 <- as.data.frame.matrix(coeftest(result3, vcov_id3))
out3 <- coef3[17:44,]

out3 <- rename.vars(out3, from="Estimate", to="pe")
out3 <- rename.vars(out3, from="Std. Error", to="se")
out3$var <- rownames(out3) 
out3$ul <- out3$pe + 1.96*out3$se 
out3$ll <- out3$pe - 1.96*out3$se 


#Argued that a politician constitutes a threat
out31 <- out3[1:4,]
out31$title <- "Argued that a politician constitutes a threat"
out31$var <- c("called for the candidate to be expelled ", "criticized the behavior", "defended the behavior", "refused to work with the candidate")

ggfeat1 <- list(coord_flip(ylim=c(-.25,.25)),  
           geom_hline(yintercept=0,linewidth=.5,colour="darkgrey",linetype="dotted"),
           geom_pointrange(mapping=aes(y=pe,x=var,ymin=ll,ymax=ul),size=.5, na.rm=T),
           scale_y_continuous(name="Change in Probability of Selecting Candidate",breaks=round(seq(-.25,.25,.05),0.5)),
           scale_x_discrete(name=""),
           theme_bw2())

out31$var <- factor(out31$var,levels=unique(out31$var)[length(out31$var):1])

p31 <- ggplot(data=out31) + ggfeat1 
pdf("fig_A1a.pdf",width=7.5,height=3)
print(p31)
dev.off()


#Argued that the government may ignore courts
result32 <- out3[5:8,]
result32$title <- "Argued that the government may ignore courts"
result32$var <- c("called for the candidate to be expelled ", "criticized the behavior", "defended the behavior", "refused to work with the candidate")

result32$var <- factor(result32$var,levels=unique(result32$var)[length(result32$var):1])
p32 <- ggplot(data=result32) + ggfeat1

pdf("fig_A1b.pdf",width=7.5,height=3)
print(p32)
dev.off()


#Argued that the government may rule without consulting Parliament
out33 <- out3[9:12,]
out33$title <- "Argued that the government may rule without consulting Parliament"
out33$var <- c("called for the candidate to be expelled ", "criticized the behavior", "defended the behavior", "refused to work with the candidate")

out33$var <- factor(out33$var,levels=unique(out33$var)[length(out33$var):1])

p33 <- ggplot(data=out33) + ggfeat1

pdf("fig_A1c.pdf",width=7.5,height=3)
print(p33)
dev.off()


#Argued that the government should exclude journalists
out34 <- out3[13:16,]
out34$title <- "Argued that the government should exclude journalists"
out34$var <- c("called for the candidate to be expelled ", "criticized the behavior", "defended the behavior", "refused to work with the candidate")

out34$var <- factor(out34$var,levels=unique(out34$var)[length(out34$var):1])
out34$var <- factor(out34$var)

p34 <- ggplot(data=out34) + ggfeat1

pdf("fig_A1d.pdf",width=7.5,height=3)
print(p34)
dev.off()


#Claimed GBP 20,000 as parliamentary expenses
out35 <- out3[21:24,] 
out35$title <- "Claimed GBP 20,000 as parliamentary expenses"
out35$var <- c("called for the candidate to be expelled ", "criticized the behavior", "defended the behavior", "refused to work with the candidate")

out35$var <- factor(out35$var,levels=unique(out35$var)[length(out35$var):1])

p35 <- ggplot(data=out35) + ggfeat1

pdf("fig_A1e.pdf",width=7.5,height=3)
print(p35)
dev.off()

#Encouraged online harassment
out36 <- out3[17:20,] 
out36$title <- "Encouraged online harassment"
out36$var <- c("called for the candidate to be expelled ", "criticized the behavior", "defended the behavior", "refused to work with the candidate")

out36$var <- factor(out36$var,levels=unique(out36$var)[length(out36$var):1])

p36 <- ggplot(data=out36) + ggfeat1 

pdf("fig_A1f.pdf",width=7.5,height=3)
print(p36)
dev.off()

#Ignored multiple messages
out37 <- out3[25:28,]
out37$title <- "Ignored multiple messages"
out37$var <- c("called for the candidate to be expelled ", "criticized the behavior", "defended the behavior", "refused to work with the candidate")

out37$var <- factor(out37$var,levels=unique(out37$var)[length(out37$var):1])

p37 <- ggplot(data=out37) + ggfeat1

pdf("fig_A1g.pdf",width=7.5,height=3)
print(p37)
dev.off()



####
##Figure A6
####

result4 <- lm(chosen ~  threat_britain + court_crisis + no_parliament + exclude_journalists + online_harrasment + expenses + ignored_messages + actor_same_party*expelled + actor_same_party*criticized + actor_same_party*defended + actor_same_party*refused_work + female + conservative + minister, data=data)
vcov_id4 <- cluster.vcov(result4, data$ID)
coef4 <- as.data.frame.matrix(coeftest(result4, vcov_id4))
out4 <- coef4[17:20,]

out4$title <- "MPs from the candidate's own party"
out4 <- rename.vars(out4, from="Estimate", to="pe")
out4 <- rename.vars(out4, from="Std. Error", to="se")
out4$var <- rownames(out4) 
out4$ul <- out4$pe + 1.96*out4$se 
out4$ll <- out4$pe - 1.96*out4$se 

out4$var <- c("called for the candidate to be expelled ", "criticized the behavior", "defended the behavior", "refused to work with the candidate")

out4$var <- factor(out4$var,levels=unique(out4$var)[length(out4$var):1])

p4 <- ggplot(data=out4) + coord_flip(ylim=c(-.05,.05)) +
      geom_hline(yintercept=0,linewidth=.5,colour="darkgrey",linetype="dotted") +
      geom_pointrange(mapping=aes(y=pe,x=var,ymin=ll,ymax=ul),size=.5, na.rm=T) +
      scale_y_continuous(name="Change in Probability of Selecting Candidate",breaks=c(-.05,0,.05)) +
      scale_x_discrete(name="") +
      theme_bw1() +
      ggtitle("MPs from the candidate's own party and ...") + theme(plot.title = element_text(hjust = -2.26, vjust=2.12, size=11))

pdf("fig_A6.pdf",width=7,height=3)
print(p4)
dev.off()



####
##Figure A7
####

data_right <- subset(data, panel=="right")
result5 <- lm(chosen ~ threat_britain + court_crisis + no_parliament + exclude_journalists + online_harrasment + expenses + ignored_messages + actor_same_party + expelled + criticized + defended + refused_work + female + conservative + minister, data=data_right)
vcov_id5 <- cluster.vcov(result5, cbind(data_right$ID))
out5 <- as.data.frame.matrix(coeftest(result5, vcov_id5))

out5 <- prep_data(out5)

out5$Panel <- "Right"

data_left <- subset(data, panel=="left")
result6 <- lm(chosen ~ threat_britain + court_crisis + no_parliament + exclude_journalists + online_harrasment + expenses + ignored_messages + actor_same_party + expelled + criticized + defended + refused_work + female + conservative + minister, data=data_left)
vcov_id6 <- cluster.vcov(result6, cbind(data_left$ID))
out6 <- as.data.frame.matrix(coeftest(result6, vcov_id6))

out6 <- prep_data(out6)

out6$Panel <- "Left"

out_panel <- rbind(out5, out6)

out_panel <- subset(out_panel, is.na(var)==F)

out_panel$var <- factor(out_panel$var,levels=unique(out_panel$var)[length(out_panel$var):1])

p_panel <- ggplot(data=out_panel) + aes(shape=Panel, color=Panel) +
           coord_flip(ylim=c(-.3,.1)) +
           geom_hline(yintercept=0,linewidth=.5,colour="darkgrey",linetype="dotted") +
           geom_pointrange(mapping=aes(y=pe,x=var,ymin=ll,ymax=ul),size=.5, position=position_dodge(width=.75), na.rm=T) +
           scale_y_continuous(name="Change in Pr(Selecting Candidate)",breaks=round(seq(-.3,.3,.1),0.5),labels=c("-.3","-.2","-.1","0", ".1", ".2", ".3")) +
           scale_x_discrete(name="") +
           scale_color_manual(values=c("Left"="black","Right"="darkgrey")) +
           theme_bw1()
  
  
pdf("fig_A7.pdf",width=9,height=5)
print(p_panel)
dev.off()



#########
##Figure A8
#########

#Round 1
data_round1 <- subset(data, round==1)
result7 <- lm(chosen ~ threat_britain + court_crisis + no_parliament + exclude_journalists + online_harrasment + expenses + ignored_messages + actor_same_party + expelled + criticized + defended + refused_work + female + conservative + minister, data=data_round1)
vcov_id7 <- cluster.vcov(result7, cbind(data_round1$ID))
out7 <- as.data.frame.matrix(coeftest(result7, vcov_id7))

out7 <- prep_data(out7)
out7$Round <- "1"

#Round 2
data_round2 <- subset(data, round==2)
result8 <- lm(chosen ~ threat_britain + court_crisis + no_parliament + exclude_journalists + online_harrasment + expenses + ignored_messages + actor_same_party + expelled + criticized + defended + refused_work + female + conservative + minister, data=data_round2)
vcov_id8 <- cluster.vcov(result8, cbind(data_round2$ID))
out8 <- as.data.frame.matrix(coeftest(result8, vcov_id8))

out8 <- prep_data(out8)
out8$Round <- "2"

#Round 3
data_round3 <- subset(data, round==3)
result9 <- lm(chosen ~ threat_britain + court_crisis + no_parliament + exclude_journalists + online_harrasment + expenses + ignored_messages + actor_same_party + expelled + criticized + defended + refused_work + female + conservative + minister, data=data_round3)
vcov_id9 <- cluster.vcov(result9, cbind(data_round3$ID))
out9 <- as.data.frame.matrix(coeftest(result9, vcov_id9))

out9 <- prep_data(out9)
out9$Round <- "3"

#Round 4
data_round4 <- subset(data, round==4)
result10 <- lm(chosen ~  threat_britain + court_crisis + no_parliament + exclude_journalists + online_harrasment + expenses + ignored_messages + actor_same_party + expelled + criticized + defended + refused_work + female + conservative + minister, data=data_round4)
vcov_id10 <- cluster.vcov(result10, cbind(data_round4$ID))
out10 <- as.data.frame.matrix(coeftest(result10, vcov_id10))

out10 <- prep_data(out10)
out10$Round <- "4"

#Round 5
data_round5 <- subset(data, round==5)
result11 <- lm(chosen ~ threat_britain + court_crisis + no_parliament + exclude_journalists + online_harrasment + expenses + ignored_messages + actor_same_party + expelled + criticized + defended + refused_work + female + conservative + minister, data=data_round5)
vcov_id11 <- cluster.vcov(result11, cbind(data_round5$ID))
out11 <- as.data.frame.matrix(coeftest(result11, vcov_id11))

out11 <- prep_data(out11)
out11$Round <- "5"

out_round <- rbind(out7, out8, out9, out10, out11)

out_round$var <- factor(out_round$var,levels=unique(out_round$var)[length(out_round$var):1])

p_round <- ggplot(data=out_round) + aes(shape=Round) +
           coord_flip(ylim=c(-.3,.1)) +
           geom_hline(yintercept=0,linewidth=.5,colour="darkgrey",linetype="dotted") +
           geom_pointrange(mapping=aes(y=pe,x=var,ymin=ll,ymax=ul),size=.5, position=position_dodge(width=.75), na.rm=T) +
           scale_y_continuous(name="Change in Pr(Selecting Candidate)",breaks=round(seq(-.3,.3,.1),0.5),labels=c("-.3","-.2","-.1","0", ".1", ".2", ".3")) +
           scale_x_discrete(name="") +
           theme_bw1()

pdf("fig_A8.pdf",width=9,height=8)
print(p_round)
dev.off()